package test;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Random;

public class Auto {

	/**
	 * @param args
	 */
	private static Random randGen = null;
	private static char[] numbersAndLetters = null;

	public static final String randomString(int length) {
	         if (length < 1) {
	             return null;
	         }
	         if (randGen == null) {
	                randGen = new Random();
	                numbersAndLetters = ("abcdefghijklmnopqrstuvwxyz" +
	                   "ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
	                  //numbersAndLetters = ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
	                 }
	         char [] randBuffer = new char[length];
	         for (int i=0; i<randBuffer.length; i++) {
	             randBuffer[i] = numbersAndLetters[randGen.nextInt(35)];
	          //randBuffer[i] = numbersAndLetters[randGen.nextInt(35)];
	         }
	         return new String(randBuffer);
	}

	private static Date randomDate(String beginDate, String endDate) {  
        try {  
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
            java.util.Date start =format.parse(beginDate);// 构造开始日期  
            java.util.Date end = format.parse(endDate);// 构造结束日期  
            // getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。  
            if (start.getTime() >= end.getTime()) {  
                return null;  
            }  
            long date = random(start.getTime(), end.getTime());  
  
            return new Date(date);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
	private static long random(long begin, long end) {  
        long rtn = begin + (long) (Math.random() * (end - begin));  
        // 如果返回的是开始时间和结束时间，则递归调用本函数查找随机值  
        if (rtn == begin || rtn == end) {  
            return random(begin, end);  
        }  
        return rtn;  
    }  
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK";
		String userName = "root";
		String password = "yangxueying";
		try {
		con = DriverManager.getConnection(url,userName, password);
		String strSql = "insert into students(name,sex,birthday,math,sport,java,password) values(?,?,?,?,?,?,?)";
		//String strSql1 = "insert into students(password) values("123456")";
		pstmt = con.prepareStatement(strSql);
		for (int i = 0; i < 100; i++) {
		   pstmt.setString(1,randomString(7));
		   pstmt.setString(2, "female");
		   Date randomDate = randomDate("2007-01-01", "2007-03-01");    
		   pstmt.setDate(3,randomDate);
		   pstmt.setDouble(4,0.0);
		   pstmt.setDouble(5,0.0);
		   pstmt.setDouble(6,0.0);
		   pstmt.setString(7,"1234");
		   pstmt.executeUpdate();
		}
		} catch (SQLException sqlE) {
			sqlE.printStackTrace();
		} finally {
			try {
				if (rs != null)
					try {
						rs.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				if (pstmt != null)
				 try {
					 pstmt.close();// 关闭语句*/
					} catch (Exception e) {
						e.printStackTrace();
					}
				if (con != null)
				 {
					try {
						con.close();// 关闭连接
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

}
